
$(function () {
    $(".sys-menu-a").on('click', function (event) {
        var menuId = $(this).data("menuid");

        $(".sys-menu-a").removeClass("active");
        $(this).addClass("active");

        $(".select-menu-item").addClass("am-hide");
        $("#sys-menu-"+menuId).removeClass("am-hide");

        $("#sys-menu-"+menuId).find(".select-menu-item-left .left-menu-btn").eq(0).click();
    });
    $(".left-menu-btn").on('click', function (event) {
        var menuId = $(this).data("menuid");
        $(".left-menu-btn").removeClass("active");
        $(".left-menu-btn .box").removeClass("active");
        $(this).addClass("active");
        $(this).find(".box").addClass("active");

        $(".cmenu-right-content").addClass("am-hide");
        $("#right-menu-"+menuId).removeClass("am-hide");
    });
    $(".cols .col .new-funmenu").on('click', function (event) {
        event.stopPropagation();
        var menuId = $(this).data("menuid");
        var menuName = $(this).data("menuname");
        var menuIcon = $(this).data("menuicon");
        var menuUrl = $(this).data("menuurl");
        var menuTarget = $(this).data("menutarget");
        if (!($.common.isNotEmpty(menuUrl) && menuUrl.indexOf("http://") != -1)) {
            menuUrl = ctx + menuUrl;
        }
        if (menuTarget == "menuBlank") {
            var dataUrl = $(this).attr('href');
            window.open(dataUrl);
            return false;
        }
        var $currentIframe = $(".moxia_iframe", window.parent.document).not(".am-hide");
        var iframe_data_id = $currentIframe.data("id");
        var $menu_tab_a = $(".fw-menu[data-id=" + iframe_data_id + "]", window.parent.document).find("a");
        $menu_tab_a.prop("title", menuName);
        $menu_tab_a.find(".fw-icon-title").removeClass().addClass("fw-icon-title " + menuIcon + " am-icon-fw");
        var $menu_tab_span = $(".fw-menu[data-id=" + iframe_data_id + "]", window.parent.document).find("span");
        $menu_tab_span.text(" " + menuName);
        $currentIframe.data("url", menuUrl);
        $currentIframe[0].src = menuUrl;
    });

    $(".connects").on('click', '.connect .new-sqlexcute', function (event) {
        event.stopPropagation();
        var connectId = $(this).data("connect_id");
        var connectName = $(this).data("connect_name");
        var connectGroup = $(this).data("connect_group");
        var databaseName = "--";

        var menuId = connectId + "-" + databaseName;
        var menuName = "";
        if((connectName == undefined || connectName == '--') && (databaseName == undefined || databaseName == '--')){
            menuName = "无标题 - 查询";
        }else if(databaseName == undefined || databaseName == '--') {
            menuName = connectName + " - 查询";
        }else {
            menuName = databaseName + " ("+connectName+")" + " - 查询";
        }
        var menuIcon = 'am-icon-database';
        var menuUrl = ctx + "osfw/dbadminer/database/sql/" + connectId + "/" + connectName + "/" + connectGroup + "/" + databaseName;

        var $currentIframe = $(".moxia_iframe", window.parent.document).not(".am-hide");
        var iframe_data_id = $currentIframe.data("id");
        var $menu_tab_a = $(".fw-menu[data-id=" + iframe_data_id + "]", window.parent.document).find("a");
        $menu_tab_a.prop("title", menuName);
        $menu_tab_a.addClass("connect-group-" + connectGroup);
        $menu_tab_a.find(".fw-icon-title").removeClass().addClass("fw-icon-title " + menuIcon + " am-icon-fw");
        var $menu_tab_span = $(".fw-menu[data-id=" + iframe_data_id + "]", window.parent.document).find("span");
        $menu_tab_span.text(" " + menuName);
        $currentIframe.data("url", menuUrl);
        $currentIframe[0].src = menuUrl;
    });
});
